package com.docman.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.docman.dbconnect.ConnectionManager;
import com.docman.domainmodel.CommentBean;
import com.docman.domainmodel.DocumentBean;
import com.docman.domainmodel.UserBean;

public class CommentDAO {

	static Connection currentCon = null;
	static ResultSet rs = null;
	
	public static void setComment(CommentBean comment) {
		
		PreparedStatement stmt = null;
		
		int docId = comment.getDocId();
		String commenter = comment.getCommenter();
		String userComment = comment.getComment();
		java.sql.Date commentDate = comment.getCommentDate();
		
		String query = "INSERT INTO COMMENT(docId, commenter, userComment, commentDate) " +
				"VALUES(?, ?, ?, ?)";
		try
		{
			ConnectionManager mgr = ConnectionManager.getInstance();
			currentCon = mgr.getConnection();

			
			
			stmt = currentCon.prepareStatement(query);  
			stmt.setInt(1, docId); 
			stmt.setString(2, commenter);
			stmt.setString(3, userComment);
			stmt.setDate(4, commentDate);
			stmt.executeUpdate();
	
			stmt.close();
		
		}

		catch(Exception e)
		{
			System.out.println("Insert Comment Failed "+e);
		}

	}
	
	public static ArrayList<CommentBean> getComment(int docId) {
		
		ArrayList<CommentBean> commentList = new ArrayList<CommentBean>();
		
		String statement ="SELECT * FROM COMMENT WHERE docId='" +docId+ "' ORDER BY commentDate DESC";

		try{
			currentCon =ConnectionManager.getInstance().getConnection();
			Statement stmt =currentCon.createStatement();
			rs= stmt.executeQuery(statement);
			
		
			while (rs.next()){
			
			CommentBean comment = new CommentBean();
			
			comment.setDocId(rs.getInt("docId"));	
			comment.setCommenter(rs.getString("commenter"));
			comment.setComment(rs.getString("userComment"));
			comment.setCommentDate(rs.getDate("commentDate"));
			
			commentList.add(comment);
			
			}
			
			rs.close();
			stmt.close();
		}
		catch(Exception e){
			
			
			System.out.println("GetComment Failed "+e);
		}
		
		
		
		return commentList;
		
	}
	
}
